﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using IBll;
using IDal;

namespace Bll
{
    public abstract class BaseBll<T>:IBaseBll<T> where T:class ,new()
    {
        protected BaseBll()
        {
            SetDal();
        }
        //等待实例化
        protected IBaseDal<T> _iDal;
        public abstract void SetDal();

        /// <summary>
        /// 2.0 数据仓储接口（相当于数据层工厂，可以创建所有的数据子类对象）
        /// </summary>
        private IDbSession _dbSession;
        public IDbSession DbSession
        {
            get
            {
                if (_dbSession == null)
                {
                    ////1.读取配置文件
                    //string strFactoryDll = ConfigurationManager.AppSettings["DBSessionFatoryDLL"];
                    //string strFactoryType = ConfigurationManager.AppSettings["DBSessionFatory"];
                    ////2.1通过反射创建 DBSessionFactory 工厂对象
                    //Assembly dalDLL = Assembly.LoadFrom(strFactoryDll);
                    //Type typeDbSessionFatory = dalDLL.GetType(strFactoryType);
                    //IDbSessionFactory sessionFactory = Activator.CreateInstance(typeDbSessionFatory) as IDbSessionFactory;
                    
                    //2.根据配置文件内容 使用 DI层里的Spring.Net 创建 DBSessionFactory 工厂对象
                    IDbSessionFactory sessionFactory = DI.SpringHelper.GetObject<IDbSessionFactory>("DbSessionFactory");
                    //3.通过 工厂 创建 DBSession对象
                    if (sessionFactory!=null)
                    {
                        _dbSession = sessionFactory.GetDbSession(); 
                    }
                }
                return _dbSession;
            }
        }

        public int Add(T model)
        {
            return _iDal.Add(model);
        }

        public int Delete(T model)
        {
            return _iDal.Delete(model);
        }

        public int DeleteBy(System.Linq.Expressions.Expression<Func<T, bool>> delWhere)
        {
            return _iDal.DeleteBy(delWhere);
        }

        public int Modify(T model, params string[] proNames)
        {
            return _iDal.Modify(model, proNames);
        }

        public int ModifyBy(T model, System.Linq.Expressions.Expression<Func<T, bool>> whereLambda, params string[] modifiedProNames)
        {
            return _iDal.ModifyBy(model, whereLambda, modifiedProNames);
        }

        #region 5.0 获取全部列表 +public IQueryable<T> GetAll()

        /// <summary>
        /// 5.0 根据条件查询 +IQueryable<T> GetAll();
        /// </summary>
        public IQueryable<T> GetAll()
        {
            return _iDal.GetAll();
        }
        #endregion

        public IQueryable<T> GetListBy(System.Linq.Expressions.Expression<Func<T, bool>> whereLambda)
        {
            return _iDal.GetListBy(whereLambda);
        }

        public IQueryable<T> GetListBy<TKey>(System.Linq.Expressions.Expression<Func<T, bool>> whereLambda, System.Linq.Expressions.Expression<Func<T, TKey>> orderLambda)
        {
            return _iDal.GetListBy(whereLambda,orderLambda);
        }

        public IQueryable<T> GetPagedList<TKey>(int pageIndex, int pageSize, Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderBy, out int count)
        {
            return _iDal.GetPagedList(pageIndex, pageSize, whereLambda, orderBy, out count);
        }
    }
}
